﻿using System.Collections;
using DG.Tweening;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class PlayerLookAt : LatentActionNodeBase
    {
        [RequiredField]
        public BBParameter<string> pointName;
        public BBParameter<Vector3> cameraAngle = Vector3.zero;
        public BBParameter<float> speed = 1;
        public BBParameter<Ease> easeType = Ease.Linear;
        
        protected bool tweenComplete = false;
        
        public IEnumerator Invoke()
        {
            // 玩家朝向点
            var bp = BlueprintUtils.GetBlueprintPoint(pointName.value);
            if (bp == null) yield break;
            BlueprintUtils.PlayerController.Face2Position(bp.position,false);
            yield return new WaitForSeconds(Time.fixedDeltaTime * 2);
            
            tweenComplete = false;
            BlueprintUtils.PlayerController.inputCamera.TweenCameraAngle(cameraAngle.value,speed.value,easeType.value,
                () =>
                {
                    tweenComplete = true;
                });
            
            yield return new UnityEngine.WaitUntil(() =>
            {
                return tweenComplete;
            });
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }
    }
}